package smashedapples.google.datastore
{
	import mx.rpc.AsyncToken;
	
	import smashedapples.core.NonVisualComponent;
	import smashedapples.google.datastore.commands.DeleteCommand;
	import smashedapples.google.datastore.commands.SaveCommand;
	import smashedapples.google.datastore.events.DataStoreDeleteEvent;
	import smashedapples.google.datastore.events.DataStoreSaveEvent;
	import smashedapples.remoting.RemoteObject;
	
	
	[Event(name="deleteSuccess",type="smashedapples.google.datastore.events.DataStoreDeleteEvent")]
	
	[Event(name="success",type="smashedapples.google.datastore.events.DataStoreSaveEvent")]
	
	[DefaultProperty("queries")]
	public class DataStore extends NonVisualComponent
	{	
		
		public static const SELECT:String 		= "select";	
		public static const SAVE:String 		= "save";
		public static const DELETE:String 		= "delete";
		
		public var endpoint:String;
		public var destination:String = "datastore";
		
				
		private function get ro():RemoteObject
		{
			return new RemoteObject( this.destination, this.endpoint );
		}
		
		public function save( modelName:String, data:Object, result:Function=null, fault:Function=null ):AsyncToken
		{	
			
			var command:SaveCommand = new SaveCommand( modelName, result, fault );
				command.addEventListener( DataStoreSaveEvent.SUCCESS, handleDataStoreSaveEvent );
				
			return ro.method( DataStore.SAVE, command.result, command.fault ).send( modelName, data ); 
		}
		
		private function handleDataStoreSaveEvent( event:DataStoreSaveEvent ):void
		{
			for each( var query:Query in this.queries )
			{
				if( query.modelName == event.modelName)
				{
					query.updateSavedItem( event.data );
				} 
			}
		}
		
		
		public function select( query:Query ):AsyncToken
		{
			return ro.method( DataStore.SELECT, query.resultHandler, query.faultHandler ).send( query ); 
		}
		
		
		public function Delete( modelName:String, keys:Array, result:Function = null, fault:Function = null ):AsyncToken
		{	
			var command:DeleteCommand = new DeleteCommand( modelName, result, fault );
				command.addEventListener( DataStoreDeleteEvent.DELETE_SUCCESS, handleDeleteResponse );
			return ro.method( DataStore.DELETE, command.result, command.fault ).send( modelName, keys );
		}
		
		
		
		private function handleDeleteResponse( event:DataStoreDeleteEvent ):void
		{	
			for each( var query:Query in this.queries )
			{
				if( query.modelName == event.modelName)
				{
					query.updateDeletedItems( event.keys );
				} 
			}
		}
		
		
		[ArrayElementType("smashedapples.google.datastore.Query")]
		private var _queries:Array = [];
		
		
		public function get queries():Array
		{
			return _queries;
		}
		public function set queries( value:Array ):void
		{
			
			if( value == null )
				return;
			
			for each( var query:Query in value )
			{
				query.datastore = this;
			}
			
			this._queries = value;
		}
		
		
		
	
		
		
		
		
		
	}
}